package com.das.mechanic_base.adapter.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：zhanglei on 2019/10/11 11:45
 * <p>
 * 邮箱：916256628@qq.com
 */
public class X3HomeSearchAdapter extends RecyclerView.Adapter<X3HomeSearchAdapter.HomeSearchHolder> {

    private Context context;

    private ItemClickListener itemClickListener;

    private List<String> mList;

    public X3HomeSearchAdapter(Context context) {
        this.context = context;
        mList = new ArrayList<>();
    }

    @Override
    public HomeSearchHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new HomeSearchHolder(LayoutInflater.from(context).inflate(R.layout.x3_home_search_item, parent, false));
    }

    @Override
    public void onBindViewHolder(HomeSearchHolder holder, int position) {
        holder.tv_context.setText(mList.get(position));

        holder.itemView.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.itemClick(mList.get(position));
            }
            if (iOnSaveClick != null) {
                iOnSaveClick.iOnSaveClick(mList.get(position));
            }
        });
    }

    public void changeSearch(List<String> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class HomeSearchHolder extends RecyclerView.ViewHolder {

        private TextView tv_context;

        public HomeSearchHolder(View itemView) {
            super(itemView);

            tv_context = itemView.findViewById(R.id.tv_context);

        }
    }

    public interface ItemClickListener {
        void itemClick(String text);
    }

    public ItemClickListener getItemClickListener() {
        return itemClickListener;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    IOnSaveClick iOnSaveClick;

    public void setiOnSaveClick(IOnSaveClick iOnSaveClick) {
        this.iOnSaveClick = iOnSaveClick;
    }

    public interface IOnSaveClick {
        void iOnSaveClick(String selectText);
    }
}
